package com.base.controller;

import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.CallbackinfoesService;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

/**
 * @Date: 2019/9/29 10:49
 * @Description:
 */
@Controller
@RequestMapping("/admin")
public class content_xianluxiaohao {

    @Autowired
    private CallbackinfoesService callbackinfoesService;// 话单记录

    @Autowired
    private UsersService usersService;

    @Autowired
    private SubscribersService subscribersService;

    /**
     *  @Date: 2019/9/30 9:06
     *  @Description:  线路消耗的结果
     *
     */
    @RequestMapping("/content_xianluxiaohao")
    public ModelAndView xianluxiaohao(HttpServletRequest request, String datetime1, String datetime2,String shopname) {
        if (shopname==null||shopname.isEmpty()){
            shopname=null;
        }
        if (datetime1 == null || datetime2 == null || datetime1.equals("null") || datetime2.equals("null")) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            //获取当前月第一天：
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            String first = format.format(c.getTime());
            datetime1 = first;

            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = format.format(ca.getTime());
            datetime2 = last;
        }
        List<Users> usersList=new ArrayList<>();
        //查询出所有的OEM商家
        if (shopname!=null){
            usersList=usersService.selectByUserName(shopname);
        }else{
            usersList = usersService.selectUsersByRole(1);
        }
        ArrayList datalist = new ArrayList();
        for (Users u : usersList) {
            //根据ID去查询
            String id = u.getId();
            String name = u.getUsername();
            Map<String, Object> map1 = callbackinfoesService.countMoneyByParentID(id, datetime1, datetime2);
            //查询用户数:
            int usersNum =subscribersService.countByParentid(id,null,null,null,null,null,null,null,null,null,null);
           BigDecimal usersNums=new BigDecimal(usersNum);
            if (map1 != null) {
                map1.put("name", name);
               BigDecimal b1= new BigDecimal(map1.get("moneyoem").toString());
               BigDecimal result=b1.divide(usersNums,4,ROUND_HALF_DOWN);
               map1.put("average",result);
            } else {
                map1 = new HashMap<>();
                map1.put("name", name);
                map1.put("money", "0");
                map1.put("moneyoem", "0");
                map1.put("average","0");
            }
            datalist.add(map1);
        }
        //对数据进行排序 根据money的大小进行排序
        for (int i = 0; i < datalist.size(); i++) {
            for (int j = i + 1; j < datalist.size(); j++) {
                Map<String, Object> m1 = (Map<String, Object>) datalist.get(i);
                Map<String, Object> m2 = (Map<String, Object>) datalist.get(j);
                double money1 = 0;
                double money2 = 0;
                if (m2.get("money") != null) {
                    money1 = Double.parseDouble(m1.get("money").toString());
                }
                if (m2.get("money") != null) {
                    money2 = Double.parseDouble(m2.get("money").toString());
                }
                if (money1 < money2) {
                    datalist.set(j, m1);
                    datalist.set(i, m2);
                }
            }
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jsp/content_xianluxiaohao");
        modelAndView.addObject("shopname", shopname);
        modelAndView.addObject("datalist", datalist);
        modelAndView.addObject("datetime1", datetime1);
        modelAndView.addObject("datetime2", datetime2);
        // 设置页面进行跳转
        return modelAndView;
    }


}